package com.asktgapp.user.fragment;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.asktgapp.R;
import com.asktgapp.base.BaseFragment;

/**
 * time:2018/1/3
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class WebViewFragment extends BaseFragment {

    private int type;
    private String content;
    private WebView mWebView;
    private String url;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_webview, container, false);
        return super.onCreateView(inflater, rootView, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        url = getActivity().getIntent().getStringExtra("url");
        type = getActivity().getIntent().getIntExtra("type", 0);
        mWebView = (WebView) rootView.findViewById(R.id.webview);
        WebSettings websetting = mWebView.getSettings();
        websetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//自适应屏幕
        websetting.setJavaScriptEnabled(true);
        websetting.setSupportZoom(true);    // 设置可以支持缩放
        websetting.setBuiltInZoomControls(false);        // 设置出现缩放工具
        websetting.setAllowFileAccess(true);
        websetting.setLoadWithOverviewMode(true);
        websetting.setDefaultTextEncodingName("utf-8"); //设置文本编码
        websetting.setAppCacheEnabled(true);
        websetting.setCacheMode(WebSettings.LOAD_DEFAULT);//设置缓存模式
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                result.confirm();
                return true;
            }
        });
        if (type == 1) {
            mWebView.loadUrl(url);

        } else {
            mWebView.loadDataWithBaseURL(null, url, "text/html", "utf-8", null);
        }

    }

    @Override
    public void loadData() {

    }
}
